package sealte.wdgf.system.listener;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EAIServerRequestListener implements ServletRequestListener {
	
	private static Logger logger = LoggerFactory.getLogger(EAIServerRequestListener.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		HttpServletRequest request = (HttpServletRequest)sre.getServletRequest();
		String sessionId = request.getSession().getId();
		ServletContext application = sre.getServletContext();
		logger.info("Request(IP:"+request.getRemoteAddr()+":"+request.getRemotePort()
				+") send to \""+request.getRequestURL()+"\" is destroyed!");
		Map<String,List<HttpServletRequest>> rom = 
				(Map<String,List<HttpServletRequest>>)application.getAttribute("ROM");
		List<HttpServletRequest> requestList = rom.get(sessionId);
		requestList.remove(request);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		HttpServletRequest request= (HttpServletRequest)sre.getServletRequest();
		String sessionId = request.getSession().getId();
		ServletContext application = sre.getServletContext();
		logger.info("Request(IP:"+request.getRemoteAddr()+":"+request.getRemotePort()
				+") send to \""+request.getRequestURL()+"\" is initialized!");
		Map<String,List<HttpServletRequest>> rom = 
				(Map<String,List<HttpServletRequest>>)application.getAttribute("ROM");
		List<HttpServletRequest> requestList = rom.get(sessionId);
		requestList.add(request);
	}

}
